define(["js/jquery-3.2.1.min.js","js/ajax.js","js/bootstrap-select.js"],function(jq,ajax){
	
	var tableTpl = '<tr id="tableContent" class="tableContent">'+
			  	'<td><input type="radio" class="linkmanRadio" id="linkman" name="linkman" value="0" /></td>'+
			  	'<td><input type="input" class="form-control tableInput name" id="name" /></td>'+
			  	'<td><select id="sexSelect"  class="form-control tableInput sexSelect">'+
			  			'<option value ="男" selected="selected">男</option>'+
			  			'<option value ="女">女</option></select></td>'+
			  	'<td><input type="input" class="form-control tableInput department" id="department" /></td>'+
			  	'<td><input type="input" class="form-control tableInput job" id="job" /></td>'+
			  	'<td><input type="input" class="form-control tableInput phoneNumber" id="phoneNumber" /></td>'+
			  	'<td><input type="input" class="form-control tableInput email" id="email" /></td>'+
			  	'<td><div class="form-group">'+
			  		'<select id="dinnerTime" title="请选择" class="selectpicker show-menu-arrow form-control dinnerTime" multiple>'+
				        '<option disabled>可多选</option><option value="24">24号</option>'+
				        '<option value="25">25号</option>'+
				        '<option value="26">26号</option></select></div></td>'+
			  	'<td><div class="form-group">'+
			  		'<select id="accommodationTime" title="请选择" class="selectpicker show-menu-arrow form-control accommodationTime" multiple>'+
				        '<option disabled>可多选</option><option value="24">24号</option>'+
				        '<option value="25">25号</option>'+
				        '<option value="26">26号</option></select></div></td>'+
			  	'<td><input type="input" class="form-control tableInput remark" id="remark" />'+
			  	'<input type="input" class="id" style="display:none;" value=""  /></td>'+
			  	'<td><span id="deleteRow"  class="glyphicon glyphicon-trash deleteRow" ></span></td></tr>';

	/*var SendParameters = {
	    collegeName:"", //高校名称
	    taxNumber:"",//税号
	    peopleNumber:"", //人数
	    linkman:"",//联系人
	    name:"",//姓名
	    sex:"",//性别
	    identityNumber:"",//身份证号码
	    department:"", //部门
	    job:"", //职务
	    phoneNumber:"", //手机号码
	    email:"", //邮箱
	    dinnerTime:"", //用餐时间
	    accommodationTime:"", //住宿时间
	    accommodationDay:"", //住宿天数
	    sightseeingSite:"", // 观光地点
	    remark:"", //备注
	}*/
	

	var app={

		init:function() {
			this.submit();
			this.setNumber();
			this.checkout();
			this.search();
			this.deleteRow();
		},

		//检测输入的人数，对应增加或减少填写行数
		addRows:function(maxNum){
			/*$("#peopleNumber").change(function(){*/
			$("#peopleNumber").off("change").on("change",function(){
				var number = $("#peopleNumber").val();
				if (number>maxNum) {
					alert("人数不能超过"+maxNum+"人");
					return false;
				}
				else if (number>1){
					$(".tableContent").remove();
					for (var i = number; i > 0; i--) {
						$("table").append(tableTpl);
					}
				}
				else{
					$(".tableContent").remove();
					$("table").append(tableTpl);
				}
				app.changeVal();
				app.checkout();
				app.deleteRow();
				$('.dinnerTime').selectpicker('refresh');  
				$('.accommodationTime').selectpicker('refresh');  
			});
		},

		//提交
		submit:function(){
			$("#submit").click(function(){
				/*console.log($("input[name='linkman']:checked").val());*/
				//填写列表的数量
				var listNum = $(".tableContent").length;
				var listSendParameters = new Array();


				var linkmanRadioList = app.getFillContent(".linkmanRadio");
				if(!app.checkoutLinkMan(linkmanRadioList)){
					alert("请选择一位联系人");
					return false;
				}
				var nameList = app.getFillContent(".name");
				var sexSelectList = app.getFillContent(".sexSelect");
				var departmentList = app.getFillContent(".department");
				var jobList = app.getFillContent(".job");
				var phoneNumberList = app.getFillContent(".phoneNumber");
				var emailList = app.getFillContent(".email");
				var dinnerTimeList = app.getFillContent(".dinnerTime:even");
				var accommodationTimeList = app.getFillContent(".accommodationTime:even");
				var remarkList = app.getFillContent(".remark");
				var idList = app.getFillContent(".id");
				if (app.checkoutNull(nameList)) {
					$(".name").addClass("error");
					alert("名字不允许为空");
					return false;
				}
				else if (app.checkoutNull(departmentList)) {
					$(".department").addClass("error");
					alert("部门不允许为空");
					return false;
				}
				else if (app.checkoutNull(jobList)) {
					$(".job").addClass("error");
					alert("职务不允许为空");
					return false;
				}
				else if (app.checkoutNull(phoneNumberList)) {
					$(".phoneNumber").addClass("error");
					alert("手机号码不允许为空");
					return false;
				}
				else if (app.checkoutNull(emailList)) {
					$(".email").addClass("error");
					alert("邮箱不允许为空");
					return false;
				}
				/*else if (app.checkoutNull(accommodationDayList)) {
					$(".accommodationDay").addClass("error");
					alert("住宿天数不允许为空");
					return false;
				}*/
				else if (app.checkoutNull(accommodationTimeList)) {
					$(".accommodationTime").addClass("error");
					alert("住宿日期不允许为空")
					return false;
				}
				else if (app.checkoutNull(accommodationTimeList)) {
					$(".accommodationTime").addClass("error");
					alert("住宿日期不允许为空")
					return false;
				}
				else if (app.checkoutNull(dinnerTimeList)) {
					$(".dinnerTimeList").addClass("error");
					alert("用餐时间不允许为空")
					return false;
				}
				else if ($("#collegeName").val()==null||$("#collegeName").val()=='') {
					$("#collegeName").addClass("error");
					alert("学校名不允许为空");
					return false;
				}
				else if ($("#taxNumber").val()==null||$("#taxNumber").val()=='') {
					$("#taxNumber").addClass("error");
					alert("税号不允许为空");
					return false;
				}
				else if ($("#peopleNumber").val()==null||$("#peopleNumber").val()=='') {
					$("#peopleNumber").addClass("error");
					alert("人数不允许为空");
					return false;
				}

				//组装list<map>数据
				for (var i = 0; i < listNum; i++) {
					var SendParameters = {
					    collegeName:"", //高校名称
					    taxNumber:"",//税号
					    peopleNumber:"", //人数
					    linkman:"",//联系人
					    name:"",//姓名
					    sex:"",//性别
					    identityNumber:"",//身份证号码
					    department:"", //部门
					    job:"", //职务
					    phoneNumber:"", //手机号码
					    email:"", //邮箱
					    dinnerTime:"", //用餐时间
					    accommodationTime:"", //住宿时间
					    accommodationDay:"", //住宿天数
					    sightseeingSite:"", // 观光地点
					    remark:"", //备注
					    registerId:"",//id
					}


					SendParameters.linkman = linkmanRadioList[i];
					//如果是联系人，则填入税号、人数
					if (linkmanRadioList[i]=1) {
	   					SendParameters.taxNumber = $("#taxNumber").val();
	    				SendParameters.peopleNumber = $("#peopleNumber").val();
					}
					else{
	   					SendParameters.taxNumber = '';
	    				SendParameters.peopleNumber = '';
					}
					SendParameters.collegeName = $("#collegeName").val();
				    SendParameters.name = nameList[i];
				    SendParameters.sex = sexSelectList[i];
				    SendParameters.department = departmentList[i];
				    SendParameters.job = jobList[i];
				    SendParameters.phoneNumber = phoneNumberList[i];
				    SendParameters.email = emailList[i];
				    var dinnerTime = ''
				    for (var j = 0; j < dinnerTimeList[i].length; j++) {
				    	dinnerTime = dinnerTime+dinnerTimeList[i][j]+','
				    }
				    SendParameters.dinnerTime = dinnerTime;
				    var accommodationTime = ''
				    for (var j = 0; j < accommodationTimeList[i].length; j++) {
				    	accommodationTime = accommodationTime+accommodationTimeList[i][j]+','
				    }
				    SendParameters.accommodationTime = accommodationTime;
				    /*SendParameters.accommodationDay = '1';*/
				    SendParameters.remark = remarkList[i];
				    SendParameters.registerId = idList[i];
				    listSendParameters.push(SendParameters);
				}
				//ajax交流
				ajax.insertRegisterUserAjax(listSendParameters).then(function (res){
					alert(res);
					window.location.href="index.html"; 
				});
			});
		},

		//判断指定学校有多少个名额
		setNumber:function(){
			var SendParameters = {
				collegeName:'',
			}
			$("#collegeName").change(function() {
				if (($("#collegeName").val()!=null||$("#collegeName").val()!='')&&($("#taxNumber").val()!=null||$("#taxNumber").val()!='')) {
					$("#peopleNumber").removeAttr("disable");
					SendParameters.collegeName = $("#collegeName").val();
					ajax.getMaxNumAjax(SendParameters).then(function(res){
						app.addRows(res);
					});
				}
			});
			$("#taxNumber").change(function() {
				if (($("#collegeName").val()!=null||$("#collegeName").val()!='')&&($("#taxNumber").val()!=null||$("#taxNumber").val()!='')) {
					$("#peopleNumber").removeAttr("disabled");
					SendParameters.collegeName = $("#collegeName").val();
					ajax.getMaxNumAjax(SendParameters).then(function(res){
						app.addRows(res);
					});
				}
			});
		},

		//设置联系人radio的value为1
		changeVal:function(){
			$(".linkmanRadio").click(function(){
				$(".linkmanRadio").attr("value",0);
				$(this).attr("value","1");
			});
		},
		

		//获取填写内容
		getFillContent:function(cName){
			var result = [];
			
			$(cName).each(function(){
			    result.push($(this).val());
			});
			return result;
		},

		//校验输入框内容
		checkout:function(){
			$(".phoneNumber").change(function(){
				/*$(".phoneNumber").each(function(){
					var result = $(this).val();
					if(!(/^1(3|4|5|7|8)\d{9}$/.test(result))){ 
						$(this).addClass("error");
				        alert("手机号码填写有误，请重填");  
				        return false; 
					}
					else{
						$(this).removeClass("error");
					}
				} );*/
				var result = $(this).val();
				if(!(/^1(3|4|5|7|8)\d{9}$/.test(result))){ 
					$(this).addClass("error");
			        alert("手机号码填写有误，请重填");  
			        return false; 
				}
				else{
					$(this).removeClass("error");
				}
			});
		

			$(".email").change(function(){
				/*$(".email").each(function(){
					var result = $(this).val();
					if(!(/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(result))){ 
						$(this).addClass("error");
				        alert("邮箱填写有误，请重填");  
				        return false; 
					}
					else{
						$(this).removeClass("error");
					}
				} );*/
				var result = $(this).val();
				if(!(/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(result))){ 
						$(this).addClass("error");
				        alert("邮箱填写有误，请重填");  
				        return false; 
					}
					else{
						$(this).removeClass("error");
					}
			});

			$(".name").change(function(){
				$(".name").each(function(){
					var result = $(this).val();
					$(this).removeClass("error");
				} );
			});

			$(".department").change(function(){
				$(".department").each(function(){
					var result = $(this).val();
					$(this).removeClass("error");
					
				} );
			});

			$(".job").change(function(){
				$(".job").each(function(){
					var result = $(this).val();
					$(this).removeClass("error");
					
				} );
			});

			/*$(".accommodationDayList").change(function(){
				$(".accommodationDayList").each(function(){
					var result = $(this).val();
					$(this).removeClass("error");
				} );
			});*/

		
			$("#collegeName").change(function(){
				var result = $("#collegeName").val();
				if (result==''||result==null) {
					$("#collegeName").addClass("error");
					alert("学校名称不允许为空");
					return false;
				}
				else{
					 	SendParameters={
					 		collegeName:''
					 	}
					 	SendParameters.collegeName = result;
					 	ajax.checkRegisterAjax(SendParameters).then(function(res){
					 		if (res=="该学校已注册") {
					 			$("#collegeName").addClass("error");
					 			$("#checkTip").show();
					 			app.checkout();
					 		}
					 		else{
					 			console.log("未报名");
					 			$("#collegeName").removeClass("error");
					 			$("#checkTip").hide();
					 			app.checkout();
					 		}
					 	});
						
					}
			});


			$("#taxNumber").change(function(){
				var result = $("#taxNumber").val();
				if (result==''||result==null) {
					$("#taxNumber").addClass("error");
					alert("税号不允许为空");
					return false;
				}
				else{
						$("#taxNumber").removeClass("error");
					}
			});

			$("#peopleNumber").change(function(){
				var result = $("#peopleNumber").val();
				if (result==''||result==null) {
					$("#peopleNumber").addClass("error");
					alert("人数不允许为空");
					return false;
				}
				else{
						$("#peopleNumber").removeClass("error");
					}
			});

		},

		//传入list检查是否为空
		checkoutNull:function(list){
			for (var i=0; i<list.length; i++) {
				if(list[i]==''||list[i]==null){
					return true;
				}
			}
			return false;
		},

		//检查是否存在联系人
		checkoutLinkMan:function(list){
			for(var i = 0; i < list.length; i++){
		        if('1' == list[i]){
		            return true;
		        }
		    }
		    return false;
		},

		//删除当前行
		deleteRow:function(){
			$(".deleteRow").off("click").on("click",function(){
				if ($(this).parent().parent().find(".linkmanRadio").val()==1) {
					alert("联系人不允许删除");
				}
				else if ($(this).parent().parent().find(".linkmanRadio").val()==null||$(this).parent().parent().find(".linkmanRadio").val()=='') {
					$(this).parent().parent().remove();
					$("#peopleNumber").attr("value",$("#peopleNumber").val()-1);
				}
				else{
					console.log($(this).parent().parent().find(".id").val());
					SendParameters = {
						registerId:''
					}
					SendParameters.registerId=$(this).parent().parent().find(".id").val();
					ajax.deleteRowAjax(SendParameters).then(function(res){
						alert(res);
					});
					$(this).parent().parent().remove();
					$("#peopleNumber").attr("value",$("#peopleNumber").val()-1);
				}
				

				
			});
		},

		//搜索
		search:function(){
			var SendParameters = {
				phoneNumber:'',
			}
			$("#search").click(function(){
				if ($("#searchNum").val()==null||$("#searchNum").val()=="") {
					alert("请输入手机号码")
					return false;
				}
				SendParameters.phoneNumber = $("#searchNum").val();
				ajax.searchInfoAjax(SendParameters).then(function(res){
					if (res.length<1) {
						alert("暂无数据");
						return false;
					}
					$(".tableContent").remove();
					for (var i = 0; i < res.length; i++) {
						var check = '';
						var sex = '';
						var result = res[i];
						if (result.taxNumber!=null||result.taxNumber!='') {
							$("#taxNumber").attr("value",result.taxNumber);
						}
						if (result.collegeName!=null||result.collegeName!='') {
							$("#collegeName").attr("value",result.collegeName);
						}
						if (result.peopleNumber!=null||result.peopleNumber!='') {
							$("#peopleNumber").attr("value",result.peopleNumber);
						}
						if (result.linkman==1) {
							check = 'checked="checked"' ;
						}
						if (result.sex=='女') {
							sex = '<option value ="女" selected="selected">女</option>'+
					  				'<option value ="男">男</option></select>'
						}
						else{
							 sex = '<option value ="男" selected="selected">男</option>'+
					  				'<option value ="女">女</option></select>'
						}
						$("table").append(
						'<tr id="tableContent" class="tableContent">'+
					  	'<td><input type="radio" class="linkmanRadio" id="linkman" name="linkman"'+check+' value="'+result.linkman+'" /></td>'+
					  	'<td><input type="input" class="form-control tableInput name" id="name" value="'+result.name+'" /></td>'+
					  	'<td><select id="sexSelect"  class="form-control tableInput sexSelect">'+sex+'</td>'+
					  	'<td><input type="input" class="form-control tableInput department" id="department" value="'+result.department+'" /></td>'+
					  	'<td><input type="input" class="form-control tableInput job" id="job" value="'+result.job+'" /></td>'+
					  	'<td><input type="input" class="form-control tableInput phoneNumber" id="phoneNumber" value="'+result.phoneNumber+'" /></td>'+
					  	'<td><input type="input" class="form-control tableInput email" id="email"  value="'+result.email+'"/></td>'+
					  	'<td><div class="form-group">'+
			  				'<select id="dinnerTime" title="请选择" class="selectpicker show-menu-arrow form-control dinnerTime dt'+i+'" multiple>'+
						        '<option disabled>可多选</option><option value="24">24号</option>'+
						        '<option value="25">25号</option>'+
						        '<option value="26">26号</option></select></div></td>'+
			  			'<td><div class="form-group">'+
			  				'<select id="accommodationTime" title="请选择" class="selectpicker show-menu-arrow form-control accommodationTime at'+i+'" multiple>'+
						        '<option disabled>可多选</option><option value="24">24号</option>'+
						        '<option value="25">25号</option>'+
						        '<option value="26">26号</option></select></div></td>'+
					  	'<td><input type="input" class="form-control tableInput remark" id="remark" value="'+result.remark+'"/>'+
			  			'<input type="input" class="id" style="display:none;" value="'+result.registerId+'"  /></td>'+
			  			'<td><span id="deleteRow"  class="glyphicon glyphicon-trash deleteRow" ></span></td></tr>'
						);
						app.changeVal();
						app.checkout();
						app.deleteRow();
						$('.dinnerTime').selectpicker('refresh');  
						$('.accommodationTime').selectpicker('refresh'); 
						var dinnerTime = new Array(); //定义一数组 
						dinnerTime = result.dinnerTime.split(","); //字符分割
						var accommodationTime = new Array();
						accommodationTime = result.accommodationTime.split(",");
						$('.dt'+i).selectpicker('val', dinnerTime);
						$('.at'+i).selectpicker('val', accommodationTime);
					}
				});
			});
		},

	}
	app.init();
});